package com.yanqu.road.server.cmd.activity.summergames;

import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.activity.summergames.SummerGamesUserData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SummerGamesProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.summergames.SummerGamesModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.sync.message.SyncResponse;
import com.yanqu.road.utils.ConfigHelper;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Cmd(code = Protocol.S_SUMMER_GAMES_SEARCH_USER, desc = "查询玩家")
@CmdApi(reqMsg = SummerGamesProto.SummerGamesSearchUserReqMsg.class, respMsg = SummerGamesProto.SummerGamesSearchUserRespMsg.class)
public class SummerGamesSearchUserCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SummerGamesProto.SummerGamesSearchUserReqMsg reqMsg = SummerGamesProto.SummerGamesSearchUserReqMsg.parseFrom(packet.getBytes());

        SummerGamesConfig config = SummerGamesActivityMgr.getSummerGamesConfig();
        // 没有活动
        if (config == null || !ActivityMgr.activityInTimeAndShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        SummerGamesUserData userData = player.getModule(SummerGamesModule.class).getUserData();
        if(userData == null){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        Map<Long, Long> map = UserMgr.getUserServerByUserAliasId(reqMsg.getWatchUserAliasId(), config.getActivityId());
        if(map.isEmpty()){
            ret = GameErrorCode.E_INPUT_USER_ALIASID_ERROR;
            return;
        }

        SummerGamesProto.CrossSummerGamesSearchUserReqMsg.Builder crossMsg = SummerGamesProto.CrossSummerGamesSearchUserReqMsg.newBuilder();
        crossMsg.setActivityId(config.getActivityId());
        for (Map.Entry<Long, Long> entry : map.entrySet()) {
            String substring = String.valueOf(entry.getKey()).substring(0, 3);
            if(substring.equals(String.valueOf(player.getUserId()).substring(0, 3))) {
                crossMsg.setUserId(entry.getKey()).setServerId(entry.getValue());
                break;
            }
        }

        if(crossMsg.getUserId() == player.getUserId()){
            ret = GameErrorCode.E_QUERY_NO_SELF;
            return;
        }

        if(!config.getActivityInfo().getServerIdList().contains(crossMsg.getServerId())){
            ret = GameErrorCode.E_ACTIVITY_NOT_IN_SAME_SERVER_LIST;
            return;
        }

//        player.sendPacket(Protocol.C_SUMMER_GAMES_SEARCH_USER, crossMsg);

        if(crossMsg.getServerId() == ConfigHelper.getLong("serverId")){
            player.sendPacket(Protocol.U_SUMMER_GAMES_SEARCH_USER,  SummerGamesActivityMgr.searchUser(crossMsg.getActivityId(), crossMsg.getUserId()));
            return;
        }


        SummerGamesProto.SummerGamesSearchUserRespMsg.Builder clientMsg = SummerGamesProto.SummerGamesSearchUserRespMsg.newBuilder();
        CompletableFuture<SyncResponse> responseFuture = GamePlayerMgr.sendToGameServer(crossMsg.getServerId(), GameProtocol.S_SUMMER_GAMES_SEARCH_USER_SERVICE, player.getUserId(), crossMsg, clientMsg);
        responseFuture.thenAccept(syncResponse -> {
            if(syncResponse.getErrorCode() == 0){
                player.sendPacket(Protocol.U_SUMMER_GAMES_SEARCH_USER, clientMsg);
            }
        });
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_SUMMER_GAMES_SEARCH_USER, SummerGamesProto.SummerGamesSearchUserRespMsg.newBuilder().setRet(ret));
        }
    }
}
